<?php

namespace App\Api;

use PhalApi\Api;
use App\Domain\Zlive as Domain_Zlive;
use App\Domain\Home as Domain_Home;
use App\Domain\User as Domain_User;

header('Access-Control-Allow-Origin: *'); // 允许所有域名访问
header('Content-Type: application/json');

/**
 * 直播间
 */
class Zlive extends Api
{

    public function getRules()
    {
        return array(
            'getSDK' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
            ),
            'getLivelist' => array(
                'p' => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页数'),
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
                'country' => array('name' => 'country', 'type' => 'string', 'default' => '', 'desc' => '国家'),
                'language' => array('name' => 'language', 'type' => 'string', 'default' => '', 'desc' => '语言'),
            ),
            'getAttentionLivelist' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
                'p' => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页数'),
            ),
            'getClassLive' => array(
                'liveclassid' => array('name' => 'liveclassid', 'type' => 'int', 'default' => '0', 'desc' => '直播分类ID'),
                'p' => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页数'),
            ),
            'createRoom' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'title' => array('name' => 'title', 'type' => 'string', 'default' => '', 'desc' => '直播标题 url编码'),
                'province' => array('name' => 'province', 'type' => 'string', 'default' => '', 'desc' => '省份'),
                'city' => array('name' => 'city', 'type' => 'string', 'default' => '', 'desc' => '城市'),
                'lng' => array('name' => 'lng', 'type' => 'string', 'default' => '0', 'desc' => '经度值'),
                'lat' => array('name' => 'lat', 'type' => 'string', 'default' => '0', 'desc' => '纬度值'),
                'type' => array('name' => 'type', 'type' => 'int', 'default' => '0', 'desc' => '直播房间类型，0是普通房间，1是私密房间，2是收费房间，3是计时房间'),
                'type_val' => array('name' => 'type_val', 'type' => 'string', 'default' => '', 'desc' => '类型值'),
                'anyway' => array('name' => 'anyway', 'type' => 'int', 'default' => '0', 'desc' => '直播类型 1 PC, 0 app'),
                'liveclassid' => array('name' => 'liveclassid', 'type' => 'int', 'default' => '0', 'desc' => '直播分类ID'),
                'deviceinfo' => array('name' => 'deviceinfo', 'type' => 'string', 'default' => '', 'desc' => '设备信息'),
                'thumb' => array('name' => 'thumb', 'type' => 'string', 'desc' => '开播封面'),
                'country' => array('name' => 'country', 'type' => 'string', 'desc' => '国家'),
                'language' => array('name' => 'language', 'type' => 'string', 'desc' => '语言'),
                'free' => array('name' => 'free', 'type' => 'int', 'desc' => '免费连麦'),
            ),
            'changeLive' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'status' => array('name' => 'status', 'type' => 'int', 'require' => true, 'desc' => '直播状态 0关闭 1直播'),
            ),

            'stopRoom' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'source' => array('name' => 'source', 'type' => 'string', 'desc' => '访问来源 socekt:断联socket，app传值空'),
                'time' => array('name' => 'time', 'type' => 'string', 'desc' => '当前时间戳'),
                'sign' => array('name' => 'sign', 'type' => 'string', 'desc' => '签名'),
            ),

            'stopInfo' => array(
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),

            'checkLive' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),

            'roomCharge' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),
            'timeCharge' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),

            'enterRoom' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'mobileid' => array('name' => 'mobileid', 'type' => 'string', 'default' => '', 'desc' => '实际唯一识别码'),
            ),

            'showVideo' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'touid' => array('name' => 'touid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '上麦会员ID'),
                'pull_url' => array('name' => 'pull_url', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '连麦用户播流地址'),
            ),

            'getUserLists' => array(
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'p' => array('name' => 'p', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '页数'),
            ),

            'getPop' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'touid' => array('name' => 'touid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '对方ID'),
            ),


            'sendGift' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'giftid' => array('name' => 'giftid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '礼物ID'),
                'giftcount' => array('name' => 'giftcount', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '礼物数量'),
            ),

            'sendBarrage' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
                'content' => array('name' => 'content', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '弹幕内容'),
            ),

            'setAdmin' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'touid' => array('name' => 'touid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '对方ID'),
            ),

            'getAdminList' => array(
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
            ),


            'getVotes' => array(
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
            ),

            'setShutUp' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '用户token'),
                'touid' => array('name' => 'touid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '禁言用户ID'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'type' => array('name' => 'type', 'type' => 'int', 'default' => '0', 'desc' => '禁言类型,0永久，1本场'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'default' => '0', 'desc' => '流名，0永久'),
            ),

            'kicking' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'touid' => array('name' => 'touid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '对方ID'),
            ),

            'superStopRoom' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '会员ID'),
                'token' => array('name' => 'token', 'require' => true, 'min' => 1, 'desc' => '会员token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'type' => array('name' => 'type', 'type' => 'int', 'default' => 0, 'desc' => '关播类型 0表示关闭当前直播 1表示禁播，2表示封禁账号'),
                'banruleid' => array('name' => 'banruleid', 'type' => 'int', 'min' => 1, 'desc' => '当type=1时房间禁播规则id'),
            ),
            'searchMusic' => array(
                'key' => array('name' => 'key', 'type' => 'string', 'require' => true, 'desc' => '关键词'),
                'p' => array('name' => 'p', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '页数'),
            ),

            'getDownurl' => array(
                'audio_id' => array('name' => 'audio_id', 'type' => 'int', 'require' => true, 'desc' => '歌曲ID'),
            ),

            'getCoin' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '会员ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'min' => 1, 'desc' => '会员token'),
            ),

            'checkLiveing' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '会员ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'min' => 1, 'desc' => '会员token'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'desc' => '流名'),
            ),

            'getLiveInfo' => array(
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'desc' => '主播ID'),
            ),

            'getLiveBanRules' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'token' => array('name' => 'token', 'require' => true, 'min' => 1, 'desc' => '会员token'),
            ),

            'getUserRank' => array(
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),

            'checkUserRedis' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'token' => array('name' => 'token', 'require' => true, 'min' => 1, 'desc' => '会员token'),
                'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '主播ID'),
                'mobileid' => array('name' => 'mobileid', 'type' => 'string', 'default' => '', 'desc' => '实际唯一识别码'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),
            'setUserData' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
                'stream' => array('name' => 'stream', 'type' => 'string', 'require' => true, 'desc' => '流名'),
            ),


        );
    }

    /**
     * 获取SDK及直播配置及直播间封禁
     * @desc 用于获取SDK类型及直播配置及直播间封禁
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].live_sdk SDK类型，0金山SDK 1腾讯SDK
     * @return object info[0].android 安卓CDN配置
     * @return object info[0].ios IOS CDN配置
     * @return string msg 提示信息
     */
    public function getSDK()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $cdnset = include API_ROOT . '/config/cdnset.php';


        $cdnset['live_sdk'] = 1;
        $cdnset['live_isban'] = '0';
        $cdnset['liveban_title'] = '';

        $live_ban = \App\getLiveBan($uid);

        if ($live_ban['is_ban']) {
            $cdnset['live_isban'] = '1';
            if ($live_ban['endtime']) {
                $cdnset['liveban_title'] = \PhalApi\T('您直播间已被封禁,截止日期：') . date("Y-m-d H:i", $live_ban['endtime']);
            } else {
                $cdnset['liveban_title'] = \PhalApi\T('您直播间已被永久封禁');
            }

        }
        $rs['info'][0] = $cdnset;


        return $rs;
    }

    /**
     * 获取直播列表
     * @desc 用于获取首页直播列表
     * @return int code 操作码，0表示成功
     * @return array info
     * @return array info[0]['slide']
     * @return string info[0]['slide'][].slide_pic 图片
     * @return string info[0]['slide'][].slide_url 链接
     * @return array  info[0]['liveclass']
     * @return string info[0]['liveclass'][].id ID
     * @return string info[0]['liveclass'][].name 名称
     * @return string info[0]['liveclass'][].thumb 图标
     * @return string info[0]['liveclass'][].des 简介
     * @return string info[0]['liveclass'][].list_order 排序
     * @return array info[0]['list'] 热门直播列表
     * @return string info[0]['list'][].uid 主播id
     * @return string info[0]['list'][].avatar 主播头像
     * @return string info[0]['list'][].avatar_thumb 头像缩略图
     * @return string info[0]['list'][].user_nickname 直播昵称
     * @return string info[0]['list'][].title 直播标题
     * @return string info[0]['list'][].city 主播位置
     * @return string info[0]['list'][].stream 流名
     * @return string info[0]['list'][].pull 播流地址
     * @return string info[0]['list'][].nums 人数
     * @return string info[0]['list'][].thumb 直播封面
     * @return string info[0]['list'][].level_anchor 主播等级
     * @return string info[0]['list'][].type 直播类型
     */
    public function getLivelist()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());


        $p = \App\checkNull($this->p);
        $country = \App\checkNull($this->country);
        $language = \App\checkNull($this->language);
        //ggg
        $uid = \App\checkNull($this->uid);

        $Domain_Home = new Domain_Home();
        $where = "status='1' and slide_id='4' ";
        $slide = $Domain_Home->getSlide($where);
        //直播分类
        $liveclass = \App\getLiveClass();
        //获取热门主播
        $domain = new Domain_Zlive();
        $key = "getLivelist_" . $p . $country . $language;
        $list = $domain->getLivelist($p, $country, $language, $uid);
//		$list=\App\getcaches($key);
//		if(!$list){
//			$list = $domain->getLivelist($p, $country,$language,$uid);
//			\App\setcaches($key,$list,2);
//		}
        $rs['info'][0]['slide'] = $slide;
        $rs['info'][0]['liveclass'] = $liveclass;
        $rs['info'][0]['list'] = $list;


        return $rs;
    }

    public function getAttentionLivelist()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());


        $p = \App\checkNull($this->p);
        $uid = \App\checkNull($this->uid);

        $Domain_Home = new Domain_Home();
        $where = "status='1' and slide_id='4' ";
        $slide = $Domain_Home->getSlide($where);

        //直播分类
        $liveclass = \App\getLiveClass();


        //获取热门主播
        $domain = new Domain_Zlive();
        $list = $domain->getAttentionLivelist($p, $uid);
//		$key="getAttentionLivelist_".$p.$uid;
//		$list=\App\getcaches($key);
//		if(!$list){
//			$list = $domain->getAttentionLivelist($p,$uid);
//			\App\setcaches($key,$list,2);
//		}
        $rs['info'][0]['slide'] = $slide;
        $rs['info'][0]['liveclass'] = $liveclass;
        $rs['info'][0]['list'] = $list;


        return $rs;
    }

    /**
     * 获取分类下的直播
     * @desc 获取分类下的直播
     * @return int code 操作码 0表示成功
     * @return string msg 提示信息
     * @return array info
     * @return string info[].uid 主播id
     * @return string info[].avatar 主播头像
     * @return string info[].avatar_thumb 头像缩略图
     * @return string info[].user_nickname 直播昵称
     * @return string info[].title 直播标题
     * @return string info[].city 主播位置
     * @return string info[].stream 流名
     * @return string info[].pull 播流地址
     * @return string info[].nums 人数
     * @return string info[].distance 距离
     * @return string info[].thumb 直播封面
     * @return string info[].level_anchor 主播等级
     * @return string info[].type 直播类型
     * @return string info[].goodnum 靓号
     **/

    public function getClassLive()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $liveclassid = \App\checkNull($this->liveclassid);
        $p = \App\checkNull($this->p);

        if (!$liveclassid) {
            return $rs;
        }
        $domain = new Domain_Zlive();
        $res = $domain->getClassLive($liveclassid, $p);

        $rs['info'] = $res;
        return $rs;
    }

    /**
     * 创建开播
     * @desc 用于用户开播生成记录
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].userlist_time 用户列表请求间隔
     * @return string info[0].barrage_fee 弹幕价格
     * @return string info[0].votestotal 主播映票
     * @return string info[0].stream 流名
     * @return string info[0].push 推流地址
     * @return string info[0].pull 播流地址
     * @return string info[0].chatserver socket地址
     * @return array info[0].game_switch 游戏开关
     * @return string info[0].game_switch[][0] 开启的游戏类型
     * @return string info[0].game_bankerid 庄家ID
     * @return string info[0].game_banker_name 庄家昵称
     * @return string info[0].game_banker_avatar 庄家头像
     * @return string info[0].game_banker_coin 庄家余额
     * @return string info[0].game_banker_limit 上庄限额
     * @return object info[0].liang 用户靓号信息
     * @return string info[0].liang.name 号码，0表示无靓号
     * @return object info[0].vip 用户VIP信息
     * @return string info[0].vip.type VIP类型，0表示无VIP，1表示有VIP
     * @return string info[0].guard_nums 守护数量
     * @return string info[0].thumb 直播封面
     * @return string msg 提示信息
     */
    public function createRoom()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }


        $live_ban = \App\getLiveBan($uid);

        if ($live_ban['is_ban']) {
            $rs['code'] = 1015;
            $rs['msg'] = \PhalApi\T('您暂无直播权限');
            return $rs;
        }


        $isAuthorAuth = \App\isAuthorAuth($uid);
//		if(!$isAuthorAuth){
//			$rs['code']=1002;
//			$rs['msg']=\PhalApi\T('请先进行身份认证或等待审核');
//			return $rs;
//		}

        $userinfo = \App\getUserInfo($uid);
        //APP原生上传后请求接口保存start
        $configpri = \App\getConfigPri();
        $level = \PhalApi\DI()->notorm->level
            ->where('id', $configpri['user_level'])
            ->fetchOne();
        if ($level['level'] > $userinfo['level']) {
            $rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('开播等级为V' . $level['level']);
            return $rs;
        }

        $nowtime = time();
        $showid = $nowtime;
        $starttime = $nowtime;
        $title = \App\checkNull($this->title);
        $province = \App\checkNull($this->province);
        $city = \App\checkNull($this->city);
        $lng = \App\checkNull($this->lng);
        $lat = \App\checkNull($this->lat);
        $type = \App\checkNull($this->type);
        $type_val = \App\checkNull($this->type_val);
        $anyway = \App\checkNull($this->anyway);
        $liveclassid = \App\checkNull($this->liveclassid);
        $deviceinfo = \App\checkNull($this->deviceinfo);
        $thumb_str = \App\checkNull($this->thumb);
        $language = \App\checkNull($this->language);
        $country = \App\checkNull($this->country);


        $sensitivewords = \App\checkSensitiveWords($title);
        if ($sensitivewords) {
            $rs['code'] = 10011;
            $rs['msg'] = \PhalApi\T('输入非法，请重新输入');
            return $rs;
        }


        if ($type == 1 && $type_val == '') {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('密码不能为空');
            return $rs;
        } else if ($type > 1 && $type_val <= 0) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('价格不能小于等于0');
            return $rs;
        }


        $stream = $uid . '_' . $nowtime;

        $push = \App\getTxTrtcUrl($uid, $stream, 1);
        $pull = \App\PrivateKey_tx('rtmp', $stream, 0);
        file_put_contents('./stream.bcp', $pull);
        file_put_contents('./push.bcp', $push);


        if (!$city) {
            $city = \PhalApi\T('好像在火星');
        }
        if (!$lng && $lng != 0) {
            $lng = '';
        }
        if (!$lat && $lat != 0) {
            $lat = '';
        }

        //APP原生上传后请求接口保存start
        //$configpri=\App\getConfigPri();


        $thumb = "";
        if ($thumb_str) {
            $cloudtype = $configpri['cloudtype'];
            if ($cloudtype == 1) { //七牛云存储
                $thumb = $thumb_str . '?imageView2/2/w/600/h/600';
            } else {
                $thumb = $thumb_str;
            }
        }


        // 主播VIP
        $vip = \App\getUserVip($uid);
        $info['vip'] = $vip;

        $dataroom = array(
            "uid" => $uid,
            "showid" => $showid,
            "starttime" => $starttime,
            "title" => $title,
            "province" => $province,
            "city" => $city,
            "stream" => $stream,
            "thumb" => $thumb,
            "pull" => $pull,
            "lng" => $lng,
            "lat" => $lat,
            "type" => $type,
            "type_val" => $type_val,
            "isvideo" => 0,
            "islive" => 1,
            "anyway" => $anyway,
            "liveclassid" => $liveclassid,
            "deviceinfo" => $deviceinfo,
            "hotvotes" => 0,
            "pkuid" => 0,
            "pkstream" => '',
            "recommend_val" => $userinfo['recommend_val'],
            "language" => $language,
            "country" => $country,
        );

        // return json_encode($dataroom);
        $domain = new Domain_Zlive();
        $result = $domain->createRoom($uid, $dataroom);

        if ($result === false) {
            $rs['code'] = 1011;
            $rs['msg'] = \PhalApi\T('开播失败，请重试');
            return $rs;
        }

        $userinfo['usertype'] = 50;
        $userinfo['sign'] = '0';
        $userinfo['lang'] = \PhalApi\DI()->lang;
        \App\setcache($token, $userinfo);


        $votestotal = $domain->getVotes($uid);

        $info['userlist_time'] = $configpri['userlist_time'];
        $info['barrage_fee'] = $configpri['barrage_fee'];
        $info['chatserver'] = $configpri['chatserver'];

        $info['votestotal'] = $votestotal;
        $info['stream'] = $stream;
        $info['push'] = $push;
        $info['pull'] = $pull;


        // 腾讯APPID
        $info['tx_appid'] = $configpri['tx_appid'];


        // 敏感词集合
        $dirtyarr = array();
        if ($configpri['sensitive_words']) {
            $dirtyarr = explode(',', $configpri['sensitive_words']);
        }
        $info['sensitive_words'] = $dirtyarr;

        //返回直播封面
        if ($thumb) {
            $info['thumb'] = \App\get_upload_path($thumb);
        } else {
            $info['thumb'] = $userinfo['avatar_thumb'];
        }

        $rs['info'][0] = $info;
        // 清除连麦PK信息
        \App\hSet('LiveConnect', $uid, 0);
        \App\hSet('LivePK', $uid, 0);
        \App\hSet('LivePK_gift', $uid, 0);

        // 后台禁用后再启用，恢复发言
        \App\delcache($uid . 'shutup', $uid);
        return $rs;
    }


    /**
     * 修改直播状态
     * @desc 用于主播修改直播状态
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].msg 成功提示信息
     * @return string msg 提示信息
     */
    public function changeLive()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $stream = \App\checkNull($this->stream);
        $status = \App\checkNull($this->status);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $domain = new Domain_Zlive();
        $info = $domain->changeLive($uid, $stream, $status);

        $rs['info'][0]['msg'] = \PhalApi\T('成功');
        return $rs;
    }


    /**
     * 关闭直播
     * @desc 用于用户结束直播
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].msg 成功提示信息
     * @return string msg 提示信息
     */
    public function stopRoom()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $stream = \App\checkNull($this->stream);
        $source = \App\checkNull($this->source);
        $time = \App\checkNull($this->time);
        $sign = \App\checkNull($this->sign);

        $domain = new Domain_Zlive();
        $domain->stopRoom($uid, $stream);

        return $rs;

        if (!$source) { //非socket来源，app访问

            if (!$time) {
                $rs['code'] = 1001;
                $rs['msg'] = \PhalApi\T('参数错误,请重试');
                return $rs;
            }

            $now = time();
            if ($now - $time > 300) {
                $rs['code'] = 1001;
                $rs['msg'] = \PhalApi\T('参数错误');
                return $rs;
            }

            if (!$sign) {
                $rs['code'] = 1001;
                $rs['msg'] = \PhalApi\T('参数错误,请重试');
                return $rs;
            }

            $checkdata = array(
                'uid' => $uid,
                'token' => $token,
                'time' => $time,
                'stream' => $stream,
            );

            $issign = \App\checkSign($checkdata, $sign);
            if (!$issign) {
                $rs['code'] = 1001;
                $rs['msg'] = \PhalApi\T('签名错误');
                return $rs;
            }
        }

        $key = 'stopRoom_' . $stream;
        $isexist = \App\getcaches($key);
        if (!$isexist) {
            $domain = new Domain_Zlive();
            $checkToken = \App\checkToken($uid, $token);
            \App\setcaches($key, '1', 10);
            if ($checkToken == 700) {
                $domain->stopRoom($uid, $stream);
                $rs['code'] = $checkToken;
                $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
                return $rs;
            }
            $info = $domain->stopRoom($uid, $stream);

        }

        $rs['info'][0]['msg'] = \PhalApi\T('关播成功');


        return $rs;
    }

    /**
     * 直播结束信息
     * @desc 用于直播结束页面信息展示
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].nums 人数
     * @return string info[0].length 时长
     * @return string info[0].votes 映票数
     * @return string msg 提示信息
     */
    public function stopInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $stream = \App\checkNull($this->stream);

        $domain = new Domain_Zlive();
        $info = $domain->stopInfo($stream);

        $rs['info'][0] = $info;
        return $rs;
    }

    /**
     * 检查直播状态
     * @desc 用于用户进房间时检查直播状态
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].type 房间类型
     * @return string info[0].type_val 收费房间价格，默认0
     * @return string info[0].type_msg 提示信息
     * @return string info[0].live_type 房间类型 0 视频直播 1 语音聊天室
     * @return string msg 提示信息
     */
    public function checkLive()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);


        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }


        if ($uid == $liveuid) {
            $rs['code'] = 1011;
            $rs['msg'] = \PhalApi\T('不能进入自己的直播间');
            return $rs;
        }


        $domain = new Domain_Zlive();
        $info = $domain->checkLive($uid, $liveuid, $stream);

        if ($info == 1005) {
            $rs['code'] = 1005;
            $rs['msg'] = \PhalApi\T('直播已结束');
            return $rs;
        } else if ($info == 1008) {
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('您已被踢出房间');
            return $rs;
        }


        $rs['info'][0] = $info;


        return $rs;
    }


    /**
     * 房间扣费
     * @desc 用于房间扣费
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].coin 用户余额
     * @return string info[0].level 用户等级
     * @return string msg 提示信息
     */
    public function roomCharge()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $domain = new Domain_Zlive();
        $info = $domain->roomCharge($uid, $liveuid, $stream);

        if ($info == 1005) {
            $rs['code'] = 1005;
            $rs['msg'] = \PhalApi\T('直播已结束');
            return $rs;
        } else if ($info == 1006) {
            $rs['code'] = 1006;
            $rs['msg'] = \PhalApi\T('该房间非扣费房间');
            return $rs;
        } else if ($info == 1007) {
            $rs['code'] = 1007;
            $rs['msg'] = \PhalApi\T('房间费用有误');
            return $rs;
        } else if ($info == 1008) {
            $rs['code'] = 1008;
            $rs['msg'] = \PhalApi\T('余额不足');
            return $rs;
        }
        $rs['info'][0]['coin'] = $info['coin'];
        $rs['info'][0]['level'] = $info['level'];

        return $rs;
    }

    /**
     * 房间计时扣费
     * @desc 用于房间计时扣费
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].coin 用户余额
     * @return string msg 提示信息
     */
    public function timeCharge()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $domain = new Domain_Zlive();

        $key = 'timeCharge_' . $stream . '_' . $uid;
        $cache = \App\getcaches($key);
        if ($cache) {
            $coin = $domain->getUserCoin($uid);
            $rs['info'][0]['coin'] = $coin['coin'];
            return $rs;
        }

        $info = $domain->roomCharge($uid, $liveuid, $stream);

        if ($info == 1005) {
            $rs['code'] = 1005;
            $rs['msg'] = \PhalApi\T('直播已结束');
            return $rs;
        } else if ($info == 1006) {
            $rs['code'] = 1006;
            $rs['msg'] = \PhalApi\T('该房间非扣费房间');
            return $rs;
        } else if ($info == 1007) {
            $rs['code'] = 1007;
            $rs['msg'] = \PhalApi\T('房间费用有误');
            return $rs;
        } else if ($info == 1008) {
            $rs['code'] = 1008;
            $rs['msg'] = \PhalApi\T('余额不足');
            return $rs;
        }
        $rs['info'][0]['coin'] = $info['coin'];


        \App\setcaches($key, 1, 50);

        return $rs;
    }


    /**
     * 进入直播间
     * @desc 用于用户进入直播
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].votestotal 直播映票
     * @return string info[0].barrage_fee 弹幕价格
     * @return string info[0].userlist_time 用户列表获取间隔
     * @return string info[0].chatserver socket地址
     * @return string info[0].isattention 是否关注主播，0表示未关注，1表示已关注
     * @return string info[0].nums 房间人数
     * @return string info[0].pull 播流地址
     * @return string info[0].linkmic_uid 连麦用户ID，0表示未连麦
     * @return string info[0].linkmic_pull 连麦播流地址
     * @return array info[0].userlists 用户列表
     * @return object info[0].vip 用户VIP信息
     * @return string info[0].vip.type VIP类型，0表示无VIP，1表示普通VIP
     * @return object info[0].pkinfo 主播连麦/PK信息
     * @return string info[0].pkinfo.pkuid 连麦用户ID
     * @return string info[0].pkinfo.pkpull 连麦用户播流地址
     * @return string info[0].pkinfo.ifpk 是否PK
     * @return string info[0].pkinfo.pk_time 剩余PK时间（秒）
     * @return string info[0].pkinfo.pk_gift_liveuid 主播PK总额
     * @return string info[0].pkinfo.pk_gift_pkuid 连麦主播PK总额
     * @return string info[0].sensitive_words 敏感词
     * @return string msg 提示信息
     */
    public function enterRoom()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);
        $mobileid = \App\checkNull($this->mobileid);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }
        $domain = new Domain_Zlive();
        //是否禁言
        $domain->checkShut($uid, $liveuid);
        $liveinfo = $domain->getLiveInfo($liveuid);


        //获取进入房间的用户信息
        $userinfo = $this->getRoomUserInfo($uid, $liveuid, $stream);
        $userinfo['lang'] = \PhalApi\DI()->lang;

        \App\setcache($token, $userinfo);

        /* 用户列表 */
        $userlists = $this->getUserList($liveuid, $stream);

        /* 用户连麦 */
        $linkmic_uid = '0';
        $linkmic_pull = '';
        $showVideo = \App\hGet('ShowVideo', $liveuid);

        if ($showVideo) {
            $showVideo_a = json_decode($showVideo, true);
            $linkmic_uid = $showVideo_a['uid'];
            $linkmic_pull = $this->getPullWithSign($showVideo_a['pull_url']);
        }

        /* 主播连麦 */
        $pkinfo = array(
            'pkuid' => '0',
            'pkpull' => '0',
            'ifpk' => '0',
            'pk_time' => '0',
            'pk_gift_liveuid' => '0',
            'pk_gift_pkuid' => '0',
        );
        $pkuid = \App\hGet('LiveConnect', $liveuid);

        if ($pkuid) {
            $pkinfo['pkuid'] = $pkuid;
            /* 在连麦 */
            $pkpull = \App\hGet('LiveConnect_pull', $pkuid);
            $pkinfo['pkpull'] = $this->getPullWithSign($pkpull);
            $ifpk = \App\hGet('LivePK', $liveuid);
            if ($ifpk) {
                $pkinfo['ifpk'] = '1';
                $pk_time = \App\hGet('LivePK_timer', $liveuid);
                if (!$pk_time) {
                    $pk_time = \App\hGet('LivePK_timer', $pkuid);
                }
                $nowtime = time();
                if ($pk_time && $pk_time > 0 && $pk_time < $nowtime) {
                    $cha = 5 * 60 - ($nowtime - $pk_time);
                    $pkinfo['pk_time'] = (string)$cha;

                    $pk_gift_liveuid = \App\hGet('LivePK_gift', $liveuid);
                    if ($pk_gift_liveuid) {
                        $pkinfo['pk_gift_liveuid'] = (string)$pk_gift_liveuid;
                    }
                    $pk_gift_pkuid = \App\hGet('LivePK_gift', $pkuid);
                    if ($pk_gift_pkuid) {
                        $pkinfo['pk_gift_pkuid'] = (string)$pk_gift_pkuid;
                    }

                } else {
                    $pkinfo['ifpk'] = '0';
                }
            }

        }


        $configpri = \App\getConfigPri();
        $info = array(
            'votestotal' => $userlists['votestotal'],
            'barrage_fee' => $configpri['barrage_fee'],
            'userlist_time' => $configpri['userlist_time'],
            'chatserver' => $configpri['chatserver'],
            'linkmic_uid' => $linkmic_uid,
            'linkmic_pull' => $linkmic_pull,
            'nums' => $userlists['nums'],
            'vip' => $userinfo['vip'],
            'issuper' => $userinfo['issuper'],
            'usertype' => $userinfo['usertype'],
            'level' => $userinfo['level']
        );
        $info['isattention'] = \App\isAttention($uid, $liveuid);
        $info['userlists'] = $userlists['userlist'];

        /* 用户余额 */
        $usercoin = $domain->getUserCoin($uid);
        $info['coin'] = (string)$usercoin['coin'];


        /* 主播连麦/PK */
        $info['pkinfo'] = $pkinfo;


        /** 敏感词集合*/
        $dirtyarr = array();
        if ($configpri['sensitive_words']) {
            $dirtyarr = explode(',', $configpri['sensitive_words']);
        }
        $info['sensitive_words'] = $dirtyarr;


        $pull = \App\getPull($stream);
        $info['pull'] = $pull;

        $info['isudata'] = $liveinfo['isudata'];


        \App\zIncrBy('user_' . $stream, $userinfo['contribution'], (string)$uid);

        $rs['info'][0] = $info;
        return $rs;
    }

    /**
     * 连麦信息
     * @desc 用于主播同意连麦 写入redis
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string msg 提示信息
     */

    public function showVideo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $touid = \App\checkNull($this->touid);
        $pull_url = \App\checkNull($this->pull_url);


        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $data = array(
            'uid' => $touid,
            'pull_url' => $pull_url,
        );


        \App\hset('ShowVideo', $uid, json_encode($data));

        return $rs;
    }

    /**
     * 获取最新流地址
     * @desc 用于连麦获取最新流地址
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string msg 提示信息
     */

    protected function getPullWithSign($pull)
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        if ($pull == '') {
            return '';
        }
        $list1 = preg_split('/\?/', $pull);
        $originalUrl = $list1[0];

        $list = preg_split('/\//', $originalUrl);
        $url = preg_split('/\./', end($list));

        $stream = $url[0];

        $play_url = \App\getTxTrtcUrl('rtmp', $stream, 0);

        return $play_url;
    }

    /**
     * 用户列表
     * @desc 用于直播间顶部获取用户列表
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].userlist 用户列表
     * @return string info[0].nums 房间人数
     * @return string info[0].votestotal 主播映票
     * @return string info[0].guard_type 守护类型
     * @return string msg 提示信息
     */
    public function getUserLists()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);
        $p = \App\checkNull($this->p);

        /* 用户列表 */
        $info = $this->getUserList($liveuid, $stream, $p);

        $rs['info'][0] = $info;

        return $rs;
    }

    protected function getUserList($liveuid, $stream, $p = 1)
    {
        /* 用户列表 */

        $pnum = 20;
        $start = ($p - 1) * $pnum;


        $list = array();

        $uidlist = \App\zRevRange('user_' . $stream, $start, $pnum - 1, true);
        foreach ($uidlist as $k => $v) {
            $userinfo = \App\getUserInfo($k);
            $userinfo['contribution'] = (string)$v;
            if (!$$userinfo['contribution']) {
                $stream2 = explode('_', $stream);
                $showid = $stream2[1];
                //获取用户本场贡献
                $domain = new Domain_Zlive();
                $contribution = $domain->getContribut($k, $liveuid, $showid);
                $userinfo['contribution'] = $contribution;
            }
            $list[] = $userinfo;
        }
        if (!$list) {
            $list = array();
        }

        $nums = \App\zCard('user_' . $stream);
        if (!$nums) {
            $nums = 0;
        }

        $rs['userlist'] = $list;
        $rs['nums'] = (string)$nums;

        /* 主播信息 */
        $domain = new Domain_Zlive();
        $rs['votestotal'] = $domain->getVotes($liveuid);


        return $rs;
    }


    /**
     * 弹窗
     * @desc 用于直播间弹窗信息
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].consumption 消费总数
     * @return string info[0].votestotal 票总数
     * @return string info[0].follows 关注数
     * @return string info[0].fans 粉丝数
     * @return string info[0].isattention 是否关注，0未关注，1已关注
     * @return string info[0].action 操作显示，0表示自己，30表示普通用户，40表示管理员，501表示主播设置管理员，502表示主播取消管理员，60表示超管管理主播，70表示对方是超管
     * @return object info[0].vip 用户VIP信息
     * @return string info[0].vip.type VIP类型，0表示无VIP，1表示普通VIP
     * @return string msg 提示信息
     */
    public function getPop()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $liveuid = \App\checkNull($this->liveuid);
        $touid = \App\checkNull($this->touid);

        $info = \App\getUserInfo($touid);
        if (!$info) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('用户信息不存在');
            return $rs;
        }
        $info['follows'] = \App\getFollows($touid);
        $info['fans'] = \App\getFans($touid);

        $info['isattention'] = \App\isAttention($uid, $touid);
        if ($uid == $touid) {
            $info['action'] = '0';
        } else {
            $uid_admin = \App\isAdmin($uid, $liveuid);
            $touid_admin = \App\isAdmin($touid, $liveuid);

            if ($uid_admin == 40 && $touid_admin == 30) {
                $info['action'] = '40';
            } else if ($uid_admin == 50 && $touid_admin == 30) {
                $info['action'] = '501';
            } else if ($uid_admin == 50 && $touid_admin == 40) {
                $info['action'] = '502';
            } else if ($uid_admin == 60 && $touid_admin < 50) {
                $info['action'] = '40';
            } else if ($uid_admin == 60 && $touid_admin == 50) {
                $info['action'] = '60';
            } else if ($touid_admin == 60) {
                $info['action'] = '70';
            } else {
                $info['action'] = '30';
            }


        }

        //用于判断主播是否开启 查看资料的权限
        $kadmin = \App\isAdmin($uid, $liveuid);
        if (($info['action'] == 30 || $info['action'] == 0) && $kadmin == 30) {
            $domain = new Domain_Zlive();
            $result = $domain->getLiveInfo($liveuid);

            if ($result['isudata'] == 0) {
                $rs['code'] = 1002;
                $rs['msg'] = \PhalApi\T('主播已关闭查看个人资料信息');
                return $rs;
            }
        }


        $rs['info'][0] = $info;
        return $rs;
    }


    /**
     * 赠送礼物
     * @desc 用于赠送礼物
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].gifttoken 礼物token
     * @return string info[0].level 用户等级
     * @return string info[0].coin 用户余额
     * @return string msg 提示信息
     */
    public function sendGift()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);
        $giftid = \App\checkNull($this->giftid);
        $giftcount = \App\checkNull($this->giftcount);


        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }
        $domain = new Domain_Zlive();


        $result = $domain->sendGift($uid, $liveuid, $stream, $giftid, $giftcount);

        if ($result == 1001) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('余额不足');
            return $rs;
        } else if ($result == 1002) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('礼物信息不存在');
            return $rs;
        } else if ($result == 1003) {
            $rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('背包中数量不足');
            return $rs;
        } else if ($result == 1004) {
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('请选择接收礼物用户');
            return $rs;
        }

        $rs['info'][0]['gifttoken'] = $result['gifttoken'];
        $rs['info'][0]['level'] = $result['level'];
        $rs['info'][0]['coin'] = $result['coin'];

        unset($result['gifttoken']);
        unset($result['level']);
        unset($result['coin']);

        \App\setcache($rs['info'][0]['gifttoken'], $result['sendgift_info']);


        return $rs;
    }

    /**
     * 发送弹幕
     * @desc 用于发送弹幕
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].barragetoken 礼物token
     * @return string info[0].level 用户等级
     * @return string info[0].coin 用户余额
     * @return string msg 提示信息
     */
    public function sendBarrage()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $stream = \App\checkNull($this->stream);
        $content = \App\checkNull($this->content);
        $giftid = 0;
        $giftcount = 1;
        if ($content == '') {
            $rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('弹幕内容不能为空');
            return $rs;
        }

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $domain = new Domain_Zlive();
        $isshut = $domain->checkShut($uid, $liveuid);
        if ($isshut) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('已被禁言,不能发弹幕');
            return $rs;
        }

        $result = $domain->sendBarrage($uid, $liveuid, $stream, $giftid, $giftcount, $content);

        if ($result == 1001) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('余额不足');
            return $rs;
        } else if ($result == 1002) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('礼物信息不存在');
            return $rs;
        }

        $rs['info'][0]['barragetoken'] = $result['barragetoken'];
        $rs['info'][0]['level'] = $result['level'];
        $rs['info'][0]['coin'] = $result['coin'];

        unset($result['barragetoken']);

        \App\setcache($rs['info'][0]['barragetoken'], $result);

        return $rs;
    }

    /**
     * 设置/取消管理员
     * @desc 用于设置/取消管理员
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].isadmin 是否是管理员，0表示不是管理员，1表示是管理员
     * @return string msg 提示信息
     */
    public function setAdmin()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $touid = \App\checkNull($this->touid);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        if ($uid != $liveuid) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('你不是该房间主播，无权操作');
            return $rs;
        }

        $domain = new Domain_Zlive();
        $info = $domain->setAdmin($liveuid, $touid);

        if ($info == 1004) {
            $rs['code'] = 1004;
            $rs['msg'] = \PhalApi\T('最多设置5个管理员');
            return $rs;
        } else if ($info == 1003) {
            $rs['code'] = 1003;
            $rs['msg'] = \PhalApi\T('操作失败，请重试');
            return $rs;
        }

        $rs['info'][0]['isadmin'] = $info;
        return $rs;
    }

    /**
     * 管理员列表
     * @desc 用于获取管理员列表
     * @return int code 操作码，0表示成功
     * @return array info 管理员列表
     * @return array info[0]['list'] 管理员列表
     * @return array info[0]['list'][].userinfo 用户信息
     * @return string info[0]['nums'] 当前人数
     * @return string info[0]['total'] 总数
     * @return string msg 提示信息
     */
    public function getAdminList()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $liveuid = \App\checkNull($this->liveuid);


        $domain = new Domain_Zlive();
        $info = $domain->getAdminList($liveuid);

        $rs['info'][0] = $info;
        return $rs;
    }


    /**
     * 主播映票
     * @desc 用于获取主播映票
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].votestotal 映票总数
     * @return string msg 提示信息
     */
    public function getVotes()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $liveuid = \App\checkNull($this->liveuid);
        $domain = new Domain_Zlive();
        $info = $domain->getVotes($liveuid);

        $rs['info'][0]['votestotal'] = $info;
        return $rs;
    }

    /**
     * 禁言
     * @desc 用于 禁言操作
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string msg 提示信息
     */

    public function setShutUp()
    {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('禁言成功'), 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $touid = \App\checkNull($this->touid);
        $type = \App\checkNull($this->type);
        $stream = \App\checkNull($this->stream);


        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = 700;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $uidtype = \App\isAdmin($uid, $liveuid);
        if ($uidtype == 30) {
            $rs["code"] = 1001;
            $rs["msg"] = \PhalApi\T('无权操作');
            return $rs;
        }

        $touidtype = \App\isAdmin($touid, $liveuid);

        if ($touidtype == 60) {
            $rs["code"] = 1001;
            $rs["msg"] = \PhalApi\T('对方是超管，不能禁言');
            return $rs;
        }

        if ($uidtype == 40) {
            if ($touidtype == 50) {
                $rs["code"] = 1002;
                $rs["msg"] = \PhalApi\T('对方是主播，不能禁言');
                return $rs;
            }
            if ($touidtype == 40) {
                $rs["code"] = 1002;
                $rs["msg"] = \PhalApi\T('对方是管理员，不能禁言');
                return $rs;
            }


        }
        $showid = 0;
        if ($type == 1 || $stream) {
            $showid = 1;
        }
        $domain = new Domain_Zlive();
        $result = $domain->setShutUp($uid, $liveuid, $touid, $showid);

        if (!$result) {
            $rs["code"] = 1005;
            $rs["msg"] = \PhalApi\T('操作失败，请重试');
            return $rs;
        } else if ($result == 1002) {
            $rs["code"] = 1003;
            $rs["msg"] = \PhalApi\T('对方已被禁言');
            return $rs;

        }

        \App\hSet($liveuid . 'shutup', $touid, 1);

        return $rs;
    }

    /**
     * 踢人
     * @desc 用于直播间踢人
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].msg 踢出成功
     * @return string msg 提示信息
     */
    public function kicking()
    {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('踢人成功'), 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $touid = \App\checkNull($this->touid);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $admin_uid = \App\isAdmin($uid, $liveuid);
        if ($admin_uid == 30) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('无权操作');
            return $rs;
        }

        $admin_touid = \App\isAdmin($touid, $liveuid);
        if ($admin_touid == 60) {
            $rs["code"] = 1002;
            $rs["msg"] = \PhalApi\T('对方是超管，不能被踢出');
            return $rs;
        }

        if ($admin_uid != 60) {
            if ($admin_touid == 50) {
                $rs['code'] = 1001;
                $rs['msg'] = \PhalApi\T('对方是主播，不能被踢出');
                return $rs;
            }
            if ($admin_touid == 40) {
                $rs['code'] = 1002;
                $rs['msg'] = \PhalApi\T('对方是管理员，不能被踢出');
                return $rs;
            }
        }

        $domain = new Domain_Zlive();
        $result = $domain->kicking($uid, $liveuid, $touid);
        if ($result == 1002) {
            $rs["code"] = 1005;
            $rs["msg"] = \PhalApi\T('对方已被踢出');
            return $rs;
        } else if (!$result) {
            $rs["code"] = 1006;
            $rs["msg"] = \PhalApi\T('操作失败，请重试');
            return $rs;
        }

        $rs['info'][0]['msg'] = \PhalApi\T('踢出成功');
        return $rs;
    }

    /**
     * 超管关播
     * @desc 用于超管关播
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].msg 提示信息
     * @return string msg 提示信息
     */

    public function superStopRoom()
    {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('关闭成功'), 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $type = \App\checkNull($this->type);
        $banruleid = \App\checkNull($this->banruleid);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        if ($type == 1) {

            if (!$banruleid) {
                $rs['code'] = 1003;
                $rs['msg'] = \PhalApi\T('请选择封禁时间');
                return $rs;
            }

            $rules = \App\getLiveBanRules();
            $rule_ids = array_column($rules, 'id');
            if (!in_array($banruleid, $rule_ids)) {
                $rs['code'] = 1004;
                $rs['msg'] = \PhalApi\T('封禁时间不正确');
                return $rs;
            }

        } else {
            $banruleid = 0;
        }


        $domain = new Domain_Zlive();

        $result = $domain->superStopRoom($uid, $liveuid, $type, $banruleid);
        if ($result == 1001) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('你不是超管，无权操作');
            $rs['info'][0]['msg'] = \PhalApi\T('你不是超管，无权操作');
            return $rs;
        } else if ($result == 1002) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('该主播已被禁播');
            $rs['info'][0]['msg'] = \PhalApi\T('该主播已被禁播');
            return $rs;
        }
        $rs['info'][0]['msg'] = \PhalApi\T('关闭成功');

        return $rs;
    }

    /**
     * 用户余额
     * @desc 用于获取用户余额
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].coin 余额
     * @return string msg 提示信息
     */
    public function getCoin()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }


        $domain2 = new Domain_Zlive();
        $coin = $domain2->getUserCoin($uid);

        $rs['info'][0]['coin'] = $coin['coin'];
        return $rs;
    }

    /**
     * 检测房间状态
     * @desc 用于检测房间状态
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].status 状态 0关1开
     * @return string msg 提示信息
     */
    public function checkLiveing()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $stream = \App\checkNull($this->stream);

        $domain = new Domain_Zlive();

        $checkToken = \App\checkToken($uid, $token);

        if ($checkToken == 700) {

            //将主播关播
            $domain->stopRoom($uid, $stream);

            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }
        $info = $domain->checkLiveing($uid, $stream);


        $rs['info'][0]['status'] = $info;
        return $rs;
    }

    /**
     * 获取直播信息
     * @desc 用于个人中心进入直播间获取直播信息
     * @return int code 操作码，0表示成功
     * @return array info  直播信息
     * @return string msg 提示信息
     */
    public function getLiveInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $liveuid = \App\checkNull($this->liveuid);

        if ($liveuid < 1) {
            $rs['code'] = 1001;
            $rs['msg'] = \PhalApi\T('参数错误');
            return $rs;
        }


        $domain2 = new Domain_Zlive();
        $info = $domain2->getLiveInfo($liveuid);
        if (!$info) {
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('直播已结束');
            return $rs;
        }

        $rs['info'][0] = $info;
        return $rs;
    }


    /**
     * 获取封禁直播间规则列表
     * @desc 用于获取封禁直播间规则列表
     * @return int code 状态码,0表示成功
     * @return string msg 提示信息
     * @return array info 返回信息
     * @return array info[].id 规则id
     * @return array info[].name 规则名称
     * @return array info[].type 规则类型
     */
    public function getLiveBanRules()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);

        $checkToken = \App\checkToken($uid, $token);
        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }

        $rules = \App\getLiveBanRules();

        $rs['info'] = $rules;
        return $rs;
    }


    /**
     * 获取直播间用户送礼物排行榜
     * @desc 获取直播间用户送礼物排行榜
     * @return int code 状态码,0表示成功
     * @return string msg 提示信息
     * @return array info 返回信息
     * @return array info[]['contribution'] 本场直播贡献值
     */
    public function getUserRank()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $stream = \App\checkNull($this->stream);
        $liveuid = \App\checkNull($this->liveuid);


        $list = array();
        $key = 'user_' . $stream;
        $uidlist = \App\zRevRange($key, 0, 99, true);


        foreach ($uidlist as $k => $v) {
            $userinfo = \App\getUserInfo($k);
            $info = explode(".", $v);
            $userinfo['contribution'] = (string)$info[0];

            if (!$$userinfo['contribution']) {
                $stream2 = explode('_', $stream);
                $showid = $stream2[1];
                //获取用户本场贡献
                $domain = new Domain_Zlive();
                $contribution = $domain->getContribut($k, $liveuid, $showid);
                $userinfo['contribution'] = $contribution;
            }


            $list[] = $userinfo;
        }

        if (!$list) {
            $list = array();
        }

        $rs['info'] = $list;

        return $rs;
    }

    /**
     * Android用户从后台将app切回前台时检测用户信息redis是否存在
     * @desc Android用户从后台将app切回前台时检测用户信息redis是否存在
     * @return int code 状态码，0表示成功
     * @return string msg 提示信息
     * @return array info 返回信息
     * @return array info[0]['is_exist'] 是否存在redis信息 如果不存在的话Android端会重连socket
     */
    public function checkUserRedis()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $liveuid = \App\checkNull($this->liveuid);
        $mobileid = \App\checkNull($this->mobileid);
        $stream = \App\checkNull($this->stream);

        if ($uid > 0) {
            $checkToken = \App\checkToken($uid, $token);
            if ($checkToken == 700) {
                $rs['code'] = $checkToken;
                $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
                return $rs;
            }
        }
        \App\delcache($token);
        $user_redis = \App\getcache($token);

        $is_exist = "1";
        if (!$user_redis) {
            $userinfo = $this->getRoomUserInfo($uid, $liveuid, $stream);
            if ($uid < 0) {
                $userinfo['id'] = $mobileid;
            }
            \App\setcache($token, $userinfo);
            $is_exist = "0";
        }

        $rs['info'][0]['is_exist'] = $is_exist;

        return $rs;


    }


    //获取进入房间的用户信息
    private function getRoomUserInfo($uid, $liveuid, $stream = '0')
    {

        $userinfo = \App\getUserInfo($uid);

        $issuper = '0';
        if ($userinfo['issuper'] == 1) {
            $issuper = '1';
            \App\hSet('super', $userinfo['id'], '1');
        } else {
            \App\hDel('super', $userinfo['id']);
        }

        $usertype = \App\isAdmin($uid, $liveuid);
        $userinfo['usertype'] = $usertype;

        $stream2 = explode('_', $stream);
        $showid = $stream2[1];

        $contribution = '0';
        if ($showid && $uid > 0) {
            //获取用户本场贡献
            $domain = new Domain_Zlive();
            $contribution = $domain->getContribut($uid, $liveuid, $showid);
        }

        $userinfo['contribution'] = $contribution;

        return $userinfo;

    }

    /**
     * 直播间内查看用户资料开关
     * @desc 用于修改直播间内查看用户资料开关
     * @return int code 操作码，0表示成功
     * @return array info
     * @return string info[0].status 状态 0关1开
     * @return string msg 提示信息
     */
    public function setUserData()
    {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('设置成功'), 'info' => array());

        $uid = \App\checkNull($this->uid);
        $token = \App\checkNull($this->token);
        $stream = \App\checkNull($this->stream);

        $domain = new Domain_Zlive();
        $checkToken = \App\checkToken($uid, $token);

        if ($checkToken == 700) {
            $rs['code'] = $checkToken;
            $rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
            return $rs;
        }
        $info = $domain->setUserData($uid, $stream);


        $rs['info'][0]['status'] = $info;
        return $rs;
    }

}
